<template>
  <div class="reply">
    <van-cell>
      <template #title>
        <div class="cmt-wrap">
          <van-image round fit="cover" :src="currentList.aut_photo" />
          <div class="info">
            <div class="name">{{ currentList.aut_name }}</div>
            <div class="content">{{ currentList.content }}</div>
            <div class="time">
              {{ currentList.pubdate | relvTime }}
            </div>
          </div>
          <van-icon name="like-o" />
        </div>
      </template>
    </van-cell>
    <van-divider>回复列表</van-divider>

    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="item in list" :key="item.com_id">
        <template #title>
          <div class="cmt-wrap">
            <van-image round fit="cover" :src="item.aut_photo" />
            <div class="info">
              <div class="name">{{ item.aut_name }}</div>
              <div class="content">{{ item.content }}</div>
              <div class="time">
                {{ item.pubdate | relvTime }}
              </div>
            </div>
          </div>
        </template>
      </van-cell>
    </van-list>

    <!-- 输入区 -->
    <div class="cmt-input">
      <van-field
        v-model="cmtValue"
        center
        clearable
        placeholder="请输入评论内容"
      >
        <template #button>
          <van-button size="small" type="info" @click="publish"
            >发布</van-button
          >
        </template>
      </van-field>
    </div>
  </div>
</template>

<script>
import { cmtListAPI, cmtPubAPI } from "@/api";

export default {
  props: ["currentList"],
  data() {
    return {
      cmtValue: "",
      loading: false,
      finished: false,
      list: [],
      offset: null,
    };
  },
  methods: {
    async onLoad() {
      let res = await cmtListAPI({
        type: "c",
        source: this.currentList.com_id,
        offset: this.offset,
      });
      //   console.log(res);
      this.list.push(...res.data.data.results);
      // 把本次的最后一条id作为下次请求的参数
      this.offset = res.data.data.last_id;
      this.loading = false;

      //  当本次的最后一条id，等于 所有数据的结束id就意味着加载完了
      if (res.data.data.end_id == res.data.data.last_id) {
        this.finished = true;
      }
    },

    async publish() {
      //  非空判断
      if (this.cmtValue == "") {
        this.$toast("请输入内容");
        return;
      }
      let res = await cmtPubAPI({
        target: this.$route.query.id,
        content: this.cmtValue,
      });
      // console.log(res);
      // 数字unshift方法,把新内容添加到开头并返回数组新长度
      this.list.unshift(res.data.data.new_obj);

      this.cmtValue = "";
      this.$toast("内容发布成功!");
    },
  },
};
</script>

<style lang="less" scoped>
.reply {
  margin-top: 20px;
  width: 100vw;
  .cmt-wrap {
    display: flex;
    .van-image {
      width: 45px;
      height: 45px;
    }

    .info {
      margin-left: 20px;
      flex: 1;

      .content {
        margin: 8px 0;
      }
      .time {
        display: flex;
        height: 20px;
        align-items: center;
      }
    }
  }
  .cmt-input {
    position: fixed;
    left: 0;
    bottom: 0;
    width: 100%;
  }
}
</style>